﻿/* =========================================
 * Description: Create Account v1
 * 
 * Functions:
 * -Account creator for admin/employees
 * 
 * -----------------------------------------
 * Date Finished: February 16, 2010
 * Author: Nikko Fernando O. Santillan
 * =========================================
 * 
 * Description: Create Account v1.1
 * 
 * Updates:
 * -create account linked to login
 * 
  -----------------------------------------
 * Date Finished: February 21, 2010
 * Author: Nikko Fernando O. Santillan
 * =========================================
 * */

/**
 * ========================================
 * Updated by: MONICA DM. TOSINO
 * Updates: Graphical User Interface
 * Date Finished: March 1, 2010
 * ========================================
 * */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace SAHCPIMS
{
    public partial class CREATEACCOUNT : Form
    {
        public int doFlag = 0;
        LOGIN login = new LOGIN();

        public CREATEACCOUNT()
        {
            InitializeComponent();
        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            
            //this string will be the query string for the connection to the database
            string accountString =
                @"Provider=Microsoft.Jet.OLEDB.4.0;"
                + @"Data source=..\\accounts.mdb";


            OleDbConnection account = new OleDbConnection(accountString);
            DataSet detaSet = new DataSet();

            /** ============= Updated by: Monica Tosino :: Form Checking :: March 2, 2010 ============= **/
            if(usernametextBox.Text=="" || passtextBox.Text=="" 
               || verifyPassTextBox.Text=="" || employeeNumberTextBox.Text==""
               || fnameTextBox.Text=="" || mnameTextBox.Text=="" || lnameTextBox.Text=="")
            {
                   verificationMessageLabel.Text = "Input field(s) empty";
            }

            else if (employeeNumberTextBox.Text.Length != 9)
            {
                verificationMessageLabel.Text = "Invalid Employee Number.";
            }
            /** =============================== End of Update =============================== **/

            else if (!passtextBox.Text.ToString().Equals(verifyPassTextBox.Text.ToString()))
            {
                verificationMessageLabel.Text = "Message: Input Password and Verification not the same. Try Again.";
                passtextBox.ResetText();
                verifyPassTextBox.ResetText();
            }

            else
            {
                //opens account database
                account.Open();

                //for connection to the database
                try
                {
                    OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into accounts "
                                              + "values('" + usernametextBox.Text.ToString() + "','" + passtextBox.Text.ToString() + "','" + int.Parse(employeeNumberTextBox.Text) + "','" + fnameTextBox.Text + "','" + mnameTextBox.Text + "','" + lnameTextBox.Text + "')", account);

                    tagalagay.Fill(detaSet);
                    doFlag = 0;

                }
                //catches exception if the user input is invalid
                catch (FormatException)
                {

                    verificationMessageLabel.Text = "Message: Invalid Input. Try Again.";
                    doFlag = 1;

                }
                //catches exception if the input is already in the database or the input is not saved in the database
                catch (OleDbException)
                {
                    verificationMessageLabel.Text = "Message: Data not Saved in Database. Data Already Exists.";
                    doFlag = 1;

                }

                //closes accounts database
                account.Close();

                //if registration is successful, the form is disabled
                if (doFlag == 0)
                {
                    this.uname.Text = this.usernametextBox.Text;
                    this.name.Text = this.fnameTextBox.Text + " " + this.mnameTextBox.Text + " " + this.lnameTextBox.Text;
                    this.empno.Text = this.employeeNumberTextBox.Text;

                    this.signup.Visible = false;
                    this.success.Visible = true;
                }
            }
           
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            this.login.Visible = true;
            this.Visible = false;
        }

        private void Balik_Click(object sender, EventArgs e)
        {
            this.login.Visible = true;
            this.Visible = false;
        }
    }
}
